#-*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse
from blog.models import BlogContent
from blog.models import Tag
from datetime import datetime
from django.http import Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
	posts = BlogContent.objects.all()
	paginator = Paginator(posts, 5)
	page = request.GET.get('page')
	try:
		post_list = paginator.page(page)
	except PageNotAnInteger:
		post_list = paginator.page(1)
	except EmptyPage:
		post_list = paginator.paginator(paginator.num_pages)
	return render(request, 'home.html', {'post_list':post_list})

# def index(req):
	# post_list = BlogContent.objects.all()
	# return render(req, 'home.html', {'post_list':post_list})
	
def about(req):
	return render(req, 'about.html')

def detail(request, id):
    try:
        post = BlogContent.objects.get(id=str(id))
        tags = Tag.objects.all()
    except BlogContent.DoesNotExist:
        raise Http404
    return render(request, 'post.html', {'post' : post, 'tags':tags})
	
def search_tag(request, tag) :
    try:
        post_list = BlogContent.objects.filter(label__iexact = tag) #contains
    except BlogContent.DoesNotExist :
        raise Http404
    return render(request, 'tag.html', {'post_list' : post_list})

def archives(req) :
    try:
        post_list = BlogContent.objects.all()
    except BlogContent.DoesNotExist :
        raise Http404
    return render(req, 'archives.html', {'post_list' : post_list, 'error':False})
